package com.iptv.grape.domain;

import com.google.gson.annotations.Expose;
import com.iptv.grape.event.msg.Asset;
import com.iptv.grape.event.msg.AssetPlayDataMsg;
import com.iptv.grape.event.msg.EsObj;
import com.iptv.grape.event.msg.SubAsset;
import com.iptv.grape.util.AppLog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// 资产播放历史实体
public class AssetPlayData {
    // 资产ID
    private String assetID;
    // 资产编码
    private String code;
    // 名称
    private String name;
    // 资产类型 TheFilm = 电影 TVSeries = 电视剧 Variety = 综艺
    private String type;
    // 剧集总数
    private int itemNum;
    // 海报Url
    private String poster;
    // 评分
    private String score;
    // 播放地址
    private String playUrl;
    // 播放地址的剧集的ID
    private String subAssetID;
    // 当前播放的剧集下标
    private int curIndex;

    // 最后一次播放进度列表(每个剧集)
    private List<PlayProgress> playProgressList = new ArrayList<PlayProgress>();



    public int getCurLastPlayTime(){
        try {
            PlayProgress playProgress = PlayProgress.find(playProgressList, this.subAssetID);
            if (playProgress == null) {
                playProgress = new PlayProgress();
                playProgress.setPlayUrl(this.playUrl);
                playProgress.setAssetID(this.assetID);
                playProgress.setSubAssetID(this.subAssetID);
                playProgress.setLastPlayTime(0);
                playProgressList.add(playProgress);
            }
            return playProgress.getLastPlayTime();
        }
        catch (Exception e){
            //AppLog.getCurLog().out("getCurLastPlayTime 错误");
            return 0;
        }
    }
    // 设置最后的播放进度
    public void setCurLastPlayTime(int lastPlayTime){
        //SubAsset subAsset = this.subList.get(this.curIndex);
        PlayProgress playProgress = PlayProgress.find(playProgressList,this.subAssetID);
        if(playProgress == null){
            playProgress = new PlayProgress();
            playProgressList.add(playProgress);
        }
        playProgress.setPlayUrl(this.playUrl);
        playProgress.setAssetID(this.assetID);
        playProgress.setSubAssetID(this.subAssetID);
        playProgress.setLastPlayTime(lastPlayTime);
    }

    public List<PlayProgress> getPlayProgressList() {
        return playProgressList;
    }

    public void setPlayProgressList(List<PlayProgress> playProgressList) {
        this.playProgressList = playProgressList;
    }

    // 播放日期
    private Date lastPlayDate;
    // 剧集列表
    @Expose(serialize = false, deserialize = false)
    private List<EsObj> subList;

    public List<EsObj> getSubList() {
        return subList;
    }

    public void setSubList(List<EsObj> subList) {
        this.subList = subList;
    }

    public int getCurIndex() {
        return curIndex;
    }

    public void setCurIndex(int curIndex) {
        this.curIndex = curIndex;
    }

    // 构造一个AssetPlayData对象
    public static AssetPlayData toAssetPlayData(Asset asset){
        AssetPlayData assetPlayData = new AssetPlayData();
        assetPlayData.setAssetID(asset.getAssetID());
        assetPlayData.setCode(asset.getCode());
        assetPlayData.setName(asset.getName());
        assetPlayData.setType(asset.getType());
        assetPlayData.setItemNum(asset.getItemNum());
        assetPlayData.setPoster(asset.getPoster());
        assetPlayData.setScore(asset.getScore());

        assetPlayData.setPlayUrl(asset.getPlayUrl());
        assetPlayData.setSubAssetID(asset.getSubAssetID());
        //assetPlayData.setLastPlayTime(0);
        assetPlayData.setLastPlayDate(new Date());
        return assetPlayData;
    }

    // 转换成MSG回复
    public AssetPlayDataMsg toMsg(){
        AssetPlayDataMsg msg = new AssetPlayDataMsg();
        msg.setAssetID(this.getAssetID());
        msg.setCode(this.getCode());
        msg.setName(this.getName());
        msg.setPoster(this.getPoster());
        msg.setResourcesID(this.getAssetID());
        msg.setType(this.getType());
        msg.setResourcesType(this.getType());
        msg.setSubAssetID(this.getSubAssetID());
        msg.setStartTime(this.getLastPlayDate().getTime() / 1000);

        PlayProgress pro = PlayProgress.find(this.playProgressList,this.subAssetID);
        if(pro == null){
            msg.setSpeedTime(0);
        }
        else {
            msg.setSpeedTime(pro.getLastPlayTime() / 1000);
        }
        return msg;
    }

    public String getAssetID() {
        return assetID;
    }

    public void setAssetID(String assetID) {
        this.assetID = assetID;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getItemNum() {
        return itemNum;
    }

    public void setItemNum(int itemNum) {
        this.itemNum = itemNum;
    }

    public String getPoster() {
        return poster;
    }

    public void setPoster(String poster) {
        this.poster = poster;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public String getPlayUrl() {
        return playUrl;
    }

    public void setPlayUrl(String playUrl) {
        this.playUrl = playUrl;
    }

    public String getSubAssetID() {
        return subAssetID;
    }

    public void setSubAssetID(String subAssetID) {
        this.subAssetID = subAssetID;
    }


    public Date getLastPlayDate() {
        return lastPlayDate;
    }

    public void setLastPlayDate(Date lastPlayDate) {
        this.lastPlayDate = lastPlayDate;
    }
}
